<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        .list_con{
            width: 600px;
            margin: 50px auto 0;
        }
        .inputtxt{
            width: 540px;
            height: 30px;
            border: 1px solid #cccccc;
            padding: 0px;
            text-indent: 10px;
        }
        .inputbtn{
            width: 40px;
            height: 32px;
            padding: 0px;
            border: 1px solid #cccccc;
        }
        .list{
            margin: 0;
            padding: 0;
            list-style: none;
            margin-top: 20px;
        }
        .list li{
            height: 40px;
            line-height: 40px;
            border-bottom: 1px solid #cccccc;
        }
        .list li span{
            float: left;
        }
        .list a{
            float: right;
            text-decoration: none;
            margin: 0 10px;
        }
    </style>
    <!-- <script src="../JS/jquery-1.12.4.min.js"></script>
    <script>
        $(function () {
            var $txt = $('#txt1');
            var $btn = $('#btn1');
            var $list = $('#list');

            $btn.click(function () {
                var sVal = $txt.val();

                // 判断输入框是否为空
                if (sVal == '') {
                    alert('请输入内容!');
                    return;
                }
                $txt.val('');

                var $li = $('<li><span>' + sVal + '</span><a href="javascript:;" class="up"> ↑ </a><a href="javascript:;" class="down"> ↓ </a><a href="javascript:;"  class="del">删除</a></li>');
                $li.appendTo($list);

                // var $del2 = $li.find('.del');

                // $del2.click(function () {
                //     $(this).parent().remove();
                // })
            })

            // var $del = $('.del');
            // $del.click(function () {
            //     $(this).parent().remove();
            // })

            // 上述过于复杂 下述较为简单
            $list.delegate('a', 'click', function () {
                // $(this).parent().remove();
                var sTr = $(this).prop('class');

                // 删除
                if (sTr == 'del') {
                    $(this).parent().remove();
                }
                // 上移
                if (sTr == 'up') {
                    // 判断是否是第一条
                    if ($(this).parent().prev().length == 0) {
                        alert('到顶了');
                    }
                    $(this).parent().insertBefore($(this).parent().prev());
                }
                // 下移
                if (sTr == 'down') {
                    // 判断是否是最后一条
                    if ($(this).parent().next().length == 0) {
                        alert('到底了');
                    }
                    $(this).parent().insertAfter($(this).parent().next());
                }
                
            })
        })
    </script> -->
    <script src="../../JS/react.development.js"></script>
    <script src="../../JS/react-dom.development.js"></script>
    <script src="../../JS/babel.min.js"></script>
</head>
<body>
    <div id="root"></div>
    
    <script type="text/babel">
        class ToDoList extends React.Component {
            constructor(props) {
                super(props);
                this.state = {
                    aList: ['学习html', '学习css', '学习javascript'],
                    sTodo: ''
                }
            }

            fnChange(ev) {
                this.setState({
                    sTodo: ev.target.value
                })
            }

            fnAdd() {
                this.setState(function (prevState) {
                    // 判断是否为空
                    if (prevState.sTodo=='') {
                        alert('请输入内容');
                        return;
                    }
                    return {aList: [...prevState.aList, prevState.sTodo]}
                })
            }

            fnDel(i) {
                this.setState(prevState => {
                    let list = [...prevState.aList]
                    list.splice(i, 1);
                    return {aList: list};
                })
            }

            fnUp(i) {
                this.setState(prevState => {
                    if (i == 0) {
                        alert("到顶了");
                        return;
                    }
                    let list = [...prevState.aList];
                    let nowItem = list[i];
                    list.splice(i, 1);
                    list.splice(i - 1, 0, nowItem);
                    return {aList: list}
                })
            }

            fnDown(i) {
                this.setState(prevState => {
                    if (i == prevState.aList.length - 1) {
                        alert("到底了")
                    }
                    let list = [...prevState.aList];
                    let nowItem = list[i];
                    list.splice(i, 1);
                    list.splice(i + 1, 0, nowItem);
                    return {aList: list}
                })
            }

            render() {
                return(
                    <div className="list_con">
                        <h2>To do list</h2>
                        <input type="text" value={this.state.sTodo} name="" id="txt1" className="inputtxt" onChange={this.fnChange.bind(this)}/>
                        <input type="button" name="" value="增加" id="btn1" className="inputbtn" onClick={this.fnAdd.bind(this)}/>
                        <ul id="list" className="list">
                            {
                                this.state.aList.map((item, i) => {
                                    return <li key={i}><span>{item}</span><a href="javascript:;" className="up" onClick={this.fnUp.bind(this, i)}> ↑ </a><a href="javascript:;" className="down" onClick={this.fnDown.bind(this, i)}> ↓ </a><a href="javascript:;"  className="del" onClick={this.fnDel.bind(this, i)}>删除</a></li>
                                })
                            }
                        </ul>
                    </div>
                );
            }
        }

        ReactDOM.render(<ToDoList />, document.getElementById('root'))
    </script>
</body>
</html>